import sys

from PyQt6.QtWidgets import QApplication, QMainWindow, QTabWidget, QStyle

from SQLiteBase import init_database
from event.HotkeyEvent import HotkeyEvent
from event.MouseClickEvent import MouseClickEvent
from widget.SettingsTab import SettingsTab
from widget.WordDefinitionTab import WordDefinitionTab


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Auto")
        self.setFixedSize(400, 300)

        style = self.style()
        self.setWindowIcon(
            style.standardIcon(QStyle.StandardPixmap.SP_ComputerIcon)
        )

        # 创建 tab 控件
        self.tab_widget = QTabWidget()
        self.setCentralWidget(self.tab_widget)

        # 创建两个 tab 页面
        self.tab_widget.addTab(WordDefinitionTab(), "词汇")
        self.tab_widget.addTab(SettingsTab(), "设置")

        # 为标签页设置标准图标
        # self.tab_widget.setTabIcon(0, style.standardIcon(QStyle.StandardPixmap.SP_TrashIcon))
        # self.tab_widget.setTabIcon(1, style.standardIcon(QStyle.StandardPixmap.SP_DriveNetIcon))

        # 启动系统级鼠标监听
        self.mouse_event = MouseClickEvent()
        self.mouse_event.set_main_window(self)
        self.mouse_event.start_system_mouse_listener()

        # 热键监听
        self.hotkey_event = HotkeyEvent()
        self.hotkey_event.set_main_window(self)
        self.hotkey_event.start_system_hotkey_listener()


def main():
    app = QApplication(sys.argv)
    # 设置应用程序图标（关键：确保任务栏显示正确图标）
    style = app.style()
    app_icon = style.standardIcon(QStyle.StandardPixmap.SP_DirHomeIcon)
    app.setWindowIcon(app_icon)
    # Windows系统特定设置：确保任务栏图标正确显示
    if sys.platform == 'win32':
        try:
            import ctypes
            # 设置应用程序用户模型ID，确保任务栏图标独立显示
            myappid = 'mycompany.myproduct.auto.1.0'  # 唯一的应用程序ID
            ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
        except Exception as e:
            print(f"设置Windows任务栏图标ID时出错: {e}")
            pass

    # 初始化数据库
    init_database()

    window = MainWindow()
    window.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
